<template>
  <div class="container" v-bind:style="{height: windowHeight + 'px' }">
    <div class="change_password">
      <input type="text" class="change_input" placeholder="旧密码" v-model="form.oldpass">      
    </div>
    <div class="change_password">
      <input type="text" class="change_input" placeholder="请输入新的密码" v-model="form.newpass">
    </div>
    <div class="sign_out" @tap="changepass">确定</div>
  </div>
</template>

<script>
import wx from 'wx'
import Toast from '@/utils/toast'
import util from '@/utils/util'
import user from '@/services/user'
export default {
  data () {
    return {
      windowHeight: 0,
      form: {
        userid: 0,
        oldpass: '',
        newpass: ''
      },
      flag: true // 防止重复点击
    }
  },

  components: {
  },
  onLoad () {
  },
  methods: {
    async changepass () {
      let that = this
      // 防止重复点击
      if (!that.flag) {
        return
      }
      if (!util.validateEmp(that.form.oldpass)) {
        Toast('旧密码不为空', 'noicon');
        return
      }
      if (!util.validateEmp(that.form.newpass)) {
        Toast('新密码不为空', 'noicon');
        return
      }
      that.flag = false
      user.changePass(that.form).then(res => {
        that.reset()
        Toast(res.msg, 'success')
        wx.switchTab({
          url: '/pages/dynamic/dynamic'
        })
      }).catch(err => {
        that.reset()
        Toast(err.msg, 'fail')
      })
    },
    reset () {
      let that = this
      that.flag = true
      that.form.oldpass = ''
      that.form.newpass = ''
    }
  },
  created () {
    let that = this
    wx.getSystemInfo({
      success (res) {
        that.windowHeight = res.windowHeight
      }
    })
  },
  onShow () {
    // 获取用户信息
    let that = this
    that.form.userid = that.globalData.userInfo.id
  },
  onReady () {
  }
}
</script>

<style scoped>
.container {
  height: 100%;
}
.change_password {
  width: 100%;
  /* margin-top: 20rpx; */
  background: #fff url('http://mytimg.178mr.com/ps_icon.png') no-repeat;
  background-position: 48rpx 28rpx;
  background-size: 28rpx 30rpx;
  padding-left: 112rpx;
}
.change_input {
  display: block;
  width: 100%;
  height: 84rpx;
  font-size: 32rpx;
  color: rgba(51, 51, 51, 1);
}
.sign_out {
  margin: 0 auto;
  margin-top: 124rpx;
  width: 614rpx;
  height: 72rpx;
  background: #ff4444;
  border-radius: 48rpx;
  line-height: 72rpx;
  color: #fff;
  font-size: 32rpx;
  font-weight: 400;
  line-height: 72rpx;
  text-align: center;
}
</style>
